package com.ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Cat.Sir
 * Description:
 * DateTime: 2022-05-10 22:07
 */
@Controller
@RequestMapping("/file")
public class FileController {

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        //返回给前端上传文件是否成功
        String info ="上传成功";
        //获取上传的文件名
        String uploadFileName = file.getOriginalFilename();
        System.out.println("=======================");
        System.out.println("上传文件名:" + uploadFileName);
        if ("".equals(uploadFileName)) {
            info = "请上传文件";
        }
        //获取保存的路径
        String path = request.getServletContext().getRealPath("/upload");
        System.out.println("保存路径："+path);
        File fileRealPath = new File(path);
        if (!fileRealPath.exists()) {
            fileRealPath.mkdir();
        }
        //获取文件的读取流
        InputStream is = file.getInputStream();
        //获取文件的输出流
        OutputStream os = new FileOutputStream(new File(fileRealPath, uploadFileName));//文件输出流
        //设置缓存区
        int len = 0;
        byte[] buffer = new byte[1024];
        //将上传的文件保存到指定的文件夹下
        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);
            os.flush();
        }
        //关闭资源
        is.close();
        os.close();
        return info;
    }

    @RequestMapping("/down")
    public String download(String name,HttpServletResponse response, HttpServletRequest request,String fileSuffix) throws Exception {
        String info = "下载成功";
        String fileName=name+fileSuffix;
        if(fileName.isEmpty()){
            return "没有此资源文件";
        };
        //要下载的图片地址
        String path = request.getServletContext().getRealPath("/upload");
//        String fileName = "1.png";
        //1.设置response 响应头
        response.reset();//设置页面不缓存，清空buffer
        response.setCharacterEncoding("UTF-8");//字符编码
        response.setContentType("multipart/form-data");//二进制传输数据
        //设置响应头
        response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName ,"UTF-8"));
        File file = new File(path, fileName);
        //2、读取文件--输入流
        InputStream input = new FileInputStream(file);
        //3、写出文件--输入流
        OutputStream out = response.getOutputStream();

        byte[] buff = new byte[1024];
        int index = 0;
        while ((index = input.read(buff)) != -1) {
            out.write(buff, 0, index);
            out.flush();
        }
        out.close();
        input.close();
        return info;
    }
}
